//
//  ViewController.m
//  05-ScrollViewPhotoAlbum
//
//  Created by 曹曹 on 16/7/20.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"

#define kScreenBounds [UIScreen mainScreen].bounds
// 全部大写
#define SCREEN_WIDTH kScreenBounds.size.width
#define SCREEN_HEIGHT kScreenBounds.size.height
// 小写的k加驼峰命名
#define kWidth kScreenBounds.size.width
#define kHeight kScreenBounds.size.height

@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self ScrollView];
    [self PageControl];
    [self addTimer];
}

- (void)ScrollView {
    NSArray *array = [NSArray arrayWithObjects:@"7", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"1", nil];
    _scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * array.count, 0);
    _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    for (int i = 0; i < array.count; i++) {
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        // 注意imageView的frame
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        imageView.image = [UIImage imageNamed:array[i]];
        scroll.delegate = self;
        scroll.minimumZoomScale = 0.5;
        scroll.maximumZoomScale = 2;
        scroll.tag = i;
        [scroll addSubview:imageView];
        [_scrollView addSubview:scroll];
    }
    [self.view addSubview:_scrollView];
}
#pragma mark - 大scrollView走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == SCREEN_WIDTH * 8) {
        scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * 7, 0);
    }
    for (UIScrollView *scroll in scrollView.subviews) {
        if ([scroll isKindOfClass:[UIScrollView class]]) {
            scroll.zoomScale = 1;
        }
    }
    _page = scrollView.contentOffset.x / SCREEN_WIDTH - 1;
    _pageControl.currentPage = _page;
}
#pragma mark - 小scrollView走
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    _index = scrollView.tag;
    return [scrollView.subviews firstObject];
}
#pragma mark - pageControl
- (void)PageControl {
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 30, 100, 40)];
    _pageControl.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT - 20);
    _pageControl.numberOfPages = 7;
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    [_pageControl addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_pageControl];
}

- (void)changeImage:(UIPageControl *)pageControl {
    _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * (_pageControl.currentPage + 1), 0);
}

- (void)addTimer {
    NSLog(@"reAdd");
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(xchImage:) userInfo:nil repeats:YES];
}

- (void)removeTimer {
    NSLog(@"remove");
    [self.timer invalidate];
    _timer = nil;
}

- (void)xchImage:(NSTimer *)timer {
    int page = (int)self.pageControl.currentPage;
    if (page == 6) {
        page = 0;
    } else {
        page++;
    }
    NSLog(@"%d", page);
    self.scrollView.contentOffset = CGPointMake((page + 1) * SCREEN_WIDTH, 0);
    self.pageControl.currentPage = page;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self addTimer];
}


/**
 *  使用构造方法
 UIImage *image=[UIImage imageNamed:@"minion"];
 _imageview=[[UIImageView alloc]initWithImage:image];
 调用initWithImage:方法，它创建出来的imageview的宽高和图片的宽高一样
 */


/**
 *  注意：将计数器的repeats设置为YES的时候，self的引用计数会加1。因此可能会导致self（即viewController）不能release，所以，必须在viewWillAppear的时候，将计数器timer停止，否则可能会导致内存泄露。
 *   停止后，一定要将timer赋空，否则还是没有释放
 */

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
